package cn.hpclub.server.controller.admin;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import cn.hpclub.server.util.Utils;
import org.json.JSONObject;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Organization;
import cn.hpclub.server.model.Website;
import cn.hpclub.server.restful.API;
import cn.hpclub.server.service.PaymentService;
import cn.hpclub.server.service.WebsiteService;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.ResultCode;
import cn.hpclub.server.validator.admin.WebsiteValidator;

public class WebsiteController extends BaseAdminController<Website>{

    static int           UI_PAGER_NUMBER_DEFAULT = 1;           // UI默认起始页
    static int           UI_PAGER_SIZE_DEFAULT   = 10;          // UI页容量

    static String        UI_EDIT                 = "edit";      // UI编辑标识
    static String        UI_ORG                  = "org";       // UI编辑标识
    public static String KEYWORD                 = "keyword";   // 关键字
    public static String MODULEID                = "moduleId";  // 业务模块编码
    public static String PAYID                   = "payId";     // 支付方式ID
    public static String QUERYTYPE               = "queryType"; // 查询方式：精确还是模糊
    public static String CCB                     = "ccb";       // 建行支付方式
    public static String YL                      = "yl";        // 银联支付方式
    public static String ZFB                     = "zfb";       // 支付宝支付方式
    public static String WECHAT                  = "wechat";    // 微信支付方式
    public static String ABC                     = "abc";    // 微信支付方式
    public static String CODE                    = "code";      // 支付方式编码
    public static String NAME                    = "name";      // 支付方式名称

    
    public static final String  KEY_MODULEID                   = "module_id";   
    public static final String  KEY_PAYPLAT                = "pay_plat";
    public static final String  KEY_OPERATION                  = "operation";         // 当前是新增配置还是编辑配置
    public static final String  KEY_TYPE                       = "type";              // 当前操作的是哪个
    public static final String  KEY_FLOW_ID                    = "flowId";

    public void list(){
        setAttr(Constant.Ui.PAGER,
                WebsiteService.service.list(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                            getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT),
                                            getSearchInfo()));
        this.keepPara("keywords");
        render("/admin/website_list.tpl");
    }

    public void auth(){
        Page<Record> page = WebsiteService.service.getOrg();
        int org_id = this.getParaToInt(Website.ORG_ID, page.getList().get(0).getInt(Organization.ID));
        int id = getParaToInt(Website.ID, 0);

        setAttr(UI_ORG, page);
        setAttr(Website.ORG_ID, org_id);
        setAttr(Website.ID, id);
        setAttr(Constant.Ui.PAGER, WebsiteService.service
                .auth(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT), 100, id, org_id));

        setAttr(Website.WEBSITE_NAME, getPara(Website.WEBSITE_NAME, ""));

        render("/admin/website_auth.tpl");
    }

    @Before({ Tx.class })
    public void save(){
        try{
            String[] value = getPara(Constant.IDS, "").split(",");
            String[] temp = null;
            for(int i = 0; i < value.length; i++){
                temp = value[i].split("\\$");
                if(temp[3].equals("1")){
                    Db.update("insert into " + Constant.Table.ORGANIZATION_CLASS_MAP + "(" + Website.CLASS_ID + ","
                            + Website.MODULEID + "," + Website.ORG_ID + ")" + " value(?,?,?) ON DUPLICATE KEY UPDATE "
                            + Website.CLASS_ID + "=?", new Object[] { temp[0], temp[1], temp[2], temp[0] });
                } else{
                    Db.update("delete from " + Constant.Table.ORGANIZATION_CLASS_MAP + " where " + Website.CLASS_ID
                            + "=? and " + Website.MODULEID + "=? and " + Website.ORG_ID + "=?",
                              new Object[] { temp[0], temp[1], temp[2] });
                }
            }
            ajaxJsonSuccessJson("保存成功!");
        }
        catch(Exception e){
            e.printStackTrace();
            ajaxJsonSuccessJson("保存失败：" + e.getMessage());
        }
    }

    public void add(){
        Record pager;

        this.setAttr("title", "新增网点类型");
        pager = new Record();
        pager.set(Website.ID, 0);
        pager.set(Website.WEBSITE_NAME, "");

        this.setAttr(UI_EDIT, getPara(UI_EDIT, "0"));
        this.setAttr(Constant.Ui.PAGER, pager);
        render("/admin/website_add.tpl");
    }

    public void edit(){
        Record pager;

        this.setAttr("title", "编辑网点类型");
        pager = Db.paginate(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT), 100, "select *",
                            "from " + Constant.Table.ORGANIZATION_CLASS + " where " + Website.ID + "=?",
                            new Object[] { getParaToInt(Website.ID, 0) })
                .getList().get(0);

        this.setAttr(UI_EDIT, getPara(UI_EDIT, "0"));
        this.setAttr(Constant.Ui.PAGER, pager);
        render("/admin/website_add.tpl");
    }

    @Before({ WebsiteValidator.class, Tx.class })
    public void update(){
        try{
            int result = 0;

            if(getPara(UI_EDIT, "0").equals("1")){
                result = Db.update(
                                   "update " + Constant.Table.ORGANIZATION_CLASS + " set " + Website.WEBSITE_ID + "=?,"
                                           + Website.WEBSITE_NAME + "=?" + " where " + Website.ID + "=?",
                                   new Object[] { getPara(Website.WEBSITE_ID, null),
                                           getPara(Website.WEBSITE_NAME, null), getPara(Website.ID, null) });
            } else{
                result = Db.update(
                                   "insert into " + Constant.Table.ORGANIZATION_CLASS + "(" + Website.ID + ","
                                           + Website.WEBSITE_ID + "," + Website.WEBSITE_NAME + "," + Website.ENABLED
                                           + ")" + " value(?,?,?,'1')",
                                   new Object[] { getPara(Website.ID, null), getPara(Website.WEBSITE_ID, null),
                                           getPara(Website.WEBSITE_NAME, null) });
            }

            if(result > 0){

                redirect("/website/list");

            } else{
                this.setAttr("errorMessage", "保存失败!");
                render("/common/error.tpl");
                // ajaxJsonErrorMessage("保存失败!");
            }

        }
        catch(Exception e){
            e.printStackTrace();
            if(e.getMessage().contains("Duplicate entry")){
                this.setAttr("errorMessage", "保存失败:网点编号重复");
                render("/common/error.tpl");
                // ajaxJsonErrorMessage("保存失败:网点编号重复");
            } else{
                this.setAttr("errorMessage", "保存失败!");
                render("/common/error.tpl");
                // ajaxJsonErrorMessage("保存失败!");
            }

        }

    }

    /**
     * 支付方式配置
     */
    public void payMethods(){
        setAttr(CCB, Db.findFirst("select * from pay where code=?", new Object[] { PaymentService.PAYMENT_CCB }));// 建行的
        setAttr(YL, Db.findFirst("select * from pay where code=?", new Object[] { PaymentService.PAYMENT_UNION_PAY }));// 银联的
        setAttr(ZFB, Db.findFirst("select * from pay where code=?", new Object[] { PaymentService.PAYMENT_ALIPAY }));// 支付宝的
        setAttr(WECHAT, Db.findFirst("select * from pay where code=?", new Object[] { PaymentService.PAYMENT_WECHAT }));// 支付宝的
        setAttr(ABC, Db.findFirst("select * from pay where code=?", new Object[] { PaymentService.PAYMENT_ABC }));// 农行的
        render("/admin/website_pay_config.tpl");
    }

    public void paymentConfig(){
        boolean state = getParaToBoolean("state");
        String payment = getPara("payment");
        JSONObject paymentObj = getPayment(payment);
        Db.update("update pay set enabled=? where code=?", new Object[] { state, paymentObj.get(CODE) });

        ajaxJsonSuccessMessage(paymentObj.getString(NAME) + "配置成功，" + (state ? "已开启" : "已关闭"));
    }

    /**
     * @Title: getCode
     * @Description:TODO
     * @param payment
     * @return JSONObject
     */
    private JSONObject getPayment(String payment){
        JSONObject jo = new JSONObject();
        jo.put(CODE, 0);
        jo.put(NAME, "");
        switch(payment){
            case "ccb":
                jo.put(CODE, PaymentService.PAYMENT_CCB);
                jo.put(NAME, "建行支付");
                break;
            case "yl":
                jo.put(CODE, PaymentService.PAYMENT_UNION_PAY);
                jo.put(NAME, "银联支付");
                break;
            case "zfb":
                jo.put(CODE, PaymentService.PAYMENT_ALIPAY);
                jo.put(NAME, "支付宝支付");
                break;
            case "wechat":
            	jo.put(CODE, PaymentService.PAYMENT_WECHAT);
            	jo.put(NAME, "微信支付");
            	break;
            case "abc":
                jo.put(CODE, PaymentService.PAYMENT_ABC);
                jo.put(NAME, "农行支付");
                break;
            default:
                break;
        }
        return jo;
    }

    public void ccbConfig(){
        boolean ccb = getParaToBoolean("ccb");
        Db.update("update pay set enabled=? where code=15", ccb);
        ajaxJsonSuccessMessage("配置成功，" + (ccb ? "已开启" : "已关闭"));
    }

    public void toConfig(){
        int moduleId = getParaToInt("moduleId");
        // List<Record> records = WebsiteService.service.getPays(moduleId);
        List<Record> records = WebsiteService.service.getPayments(moduleId);

        setAttr("pays", records);
        /*
         * int moduleId = getParaToInt("moduleId"); setAttr("moduleId",
         * moduleId); String moduleName = getPara("moduleName");
         * setAttr("moduleName", moduleName);
         */
        keepPara();
        render("/admin/website_pay_config_pays.tpl");
    }

    public void queryWebsite(){
        int moduleId = getParaToInt(MODULEID);// 给什么模块授权
        int payId = getParaToInt(PAYID);// 给哪种支付方式
        int cityOrgId = InterfaceUtil.getOrgId();
        if(cityOrgId != InterfaceUtil.getIdTop()){// 地市级管理员取地市orgId，省级管理员取自身orgId
            cityOrgId = InterfaceUtil.getCityIdByOrgId(cityOrgId + "");
        }
        setAttr(Constant.Ui.PAGER,
                WebsiteService.service.queryWebsite(getParaToInt(Constant.Ui.PAGER_NUMBER, 1),
                                                    getParaToInt(Constant.Ui.PAGER_SIZE, 10), getPara(KEYWORD, ""),
                                                    getPara(QUERYTYPE, "fuzzy"), moduleId, payId, cityOrgId));
        keepPara();
        // setAttr("themesId", getPara("themesId"));
        // this.keepPara(KEYWORD);
        // this.keepPara(QUERYTYPE);
        render("/admin/website_pay_dialog.tpl");
    }

    // public void toAuth(){
    // int moduleId = getParaToInt("moduleId");// 给什么模块授权
    // int pay = getParaToInt("pay");// 给哪种支付方式
    //
    // keepPara();
    // // 准备回显数据
    // List<Record> websites = Db.find(
    // "select oc.*,(select pc.id from pay_config pc where pc.module_id=? and
    // pc.pay_plat=? and pc.city_org_id=? and pc.website_id=oc.id) as
    // this_enabled from organization_class oc",
    // moduleId, pay, InterfaceUtil.getCityIdByOrgId(InterfaceUtil.getOrgId() +
    // ""));
    // setAttr("websites", websites);
    // render("/admin/website_pay_config_website.tpl");
    // }

    public void toAuth(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        int moduleId = getParaToInt("moduleId");// 给什么模块授权
        int payId = getParaToInt("payId");// 给哪种支付方式

        keepPara();

        String searchType = getPara("searchType", "website");
        setAttr("searchType", searchType);
        int orgId = InterfaceUtil.getOrgId();
        if(orgId != InterfaceUtil.getIdTop()){// 地市级管理员取地市orgId，省级管理员取自身orgId
            orgId = InterfaceUtil.getCityIdByOrgId(orgId + "");
        }

        findByPageForSearch4Website("SELECT pc.id AS pid, oc.WebsiteId, oc.class_name ", WebsiteService.service
                .getPayConfigSqlExceptSelect(moduleId, payId, orgId, searchInfo));

        render("/admin/website_pay_auth_list.tpl");
    }

    public void toAuthConfig(){

    	int module_id = getParaToInt("module_id");// 给什么模块授权
        int pay_plat = getParaToInt("pay_plat");// 给哪种支付方式
        keepPara();
        setAttr(KEY_MODULEID, module_id);
        setAttr(KEY_PAYPLAT, pay_plat);
        int type = getParaToInt(KEY_TYPE, 1);// 默认tab展示地市
        setAttr(KEY_TYPE, type);
        int pageSize = getParaToInt(PAGE_SIZE, 10);
        setAttr(KEY_OPERATION, "edit");
        setAttr(KEY_FLOW_ID, 0);
        ConcurrentHashMap<String, String> map = getSearchInfo();


        if(!map.containsKey("orgId")){
            int cityOrgId = WebsiteService.service.getCityOrgIdByCurrUser();
            if(cityOrgId>0){
                map.put("orgId",String.valueOf(cityOrgId));
            }

        }

        setAttr("cityPager",
                WebsiteService.service.getCityPagerForRegular(getParaToInt("city_page_number", 1), pageSize,
                		module_id, pay_plat, map));
        setAttr("storePager",
        		WebsiteService.service.getStorePagerForRegular(getParaToInt("store_page_number", 1),
                                                                          pageSize, module_id, pay_plat,
                                                                          map));
        setAttr("memberPager",
        		WebsiteService.service.getMemberPagerForRegular(getParaToInt("member_page_number", 1),
                                                                           pageSize, module_id, pay_plat,
                                                                           map));
        setAttr("classPager",
        		WebsiteService.service
                        .getChannelClassPagerForRegular(getParaToInt("channel_class_page_number", 1), pageSize,
                        		module_id, pay_plat, map));
        setAttr("clusterPager",
        		WebsiteService.service
                        .getChannelClusterPagerForRegular(getParaToInt("channel_cluster_page_number", 1), pageSize,
                        		module_id, pay_plat, map));
        setAttr("typePager",
        		WebsiteService.service
                        .getChannelTypePagerForRegular(getParaToInt("channel_type_page_number", 1), pageSize,
                        		module_id, pay_plat, map));

        render("/admin/website_pay_auth_input.tpl");
        	
    }
    
    /**
     * 修改权限配置初始化
     */
    public void editAuthConfig(){
    	
    	int module_id = getParaToInt("module_id");
        int pay_plat = getParaToInt("pay_plat");
        setAttr(KEY_MODULEID, module_id);
        keepPara();
        setAttr(KEY_PAYPLAT, pay_plat);
        int type = getParaToInt(KEY_TYPE, 1);// 默认tab展示地市
        setAttr(KEY_TYPE, type);
        int pageSize = getParaToInt(PAGE_SIZE, 10);
        setAttr(KEY_OPERATION, "edit");
        setAttr(KEY_FLOW_ID, "");
        ConcurrentHashMap<String, String> map = getSearchInfo();


        if(!map.containsKey("orgId")){
            int cityOrgId = WebsiteService.service.getCityOrgIdByCurrUser();
            if(cityOrgId>0){
                map.put("orgId",String.valueOf(cityOrgId));
            }
        }

        setAttr("cityPager",
                WebsiteService.service.getCityPagerForRegular(getParaToInt("city_page_number", 1), pageSize,
                		module_id, pay_plat, map));
        setAttr("storePager",
        		WebsiteService.service.getStorePagerForRegular(getParaToInt("store_page_number", 1),
                                                                          pageSize, module_id, pay_plat,
                                                                          map));
        setAttr("memberPager",
        		WebsiteService.service.getMemberPagerForRegular(getParaToInt("member_page_number", 1),
                                                                           pageSize, module_id, pay_plat,
                                                                           map));
        setAttr("classPager",
        		WebsiteService.service
                        .getChannelClassPagerForRegular(getParaToInt("channel_class_page_number", 1), pageSize,
                        		module_id, pay_plat, map));
        setAttr("clusterPager",
        		WebsiteService.service
                        .getChannelClusterPagerForRegular(getParaToInt("channel_cluster_page_number", 1), pageSize,
                        		module_id, pay_plat, map));
        setAttr("typePager",
        		WebsiteService.service
                        .getChannelTypePagerForRegular(getParaToInt("channel_type_page_number", 1), pageSize,
                        		module_id, pay_plat, map));
        render("/admin/website_pay_auth_input.tpl");
    }
    
    /**
     * 获取门店数据展示对话框
     */
    public void dialogCity(){
        int pageNumber = getParaToInt(Constant.Ui.PAGER_NUMBER, 1);
        int pageSize = getParaToInt(Constant.Ui.PAGER_SIZE, 10);

        String operation = getPara(KEY_OPERATION);
        setAttr(KEY_OPERATION, operation);
        String flowId = getPara(KEY_FLOW_ID);
        setAttr(KEY_FLOW_ID, flowId);

        int module_id = getParaToInt(KEY_MODULEID, 0);
        int pay_plat = getParaToInt(KEY_PAYPLAT, 0);
        setAttr(KEY_MODULEID, module_id);
        setAttr(KEY_PAYPLAT, pay_plat);

        ConcurrentHashMap<String, String> map = getSearchInfo();
      
        setAttr(Constant.Ui.PAGER, WebsiteService.service.getCity(pageNumber, pageSize, map, operation,
        		module_id, pay_plat, flowId));
        render("/admin/website_pay_auth_dialog_city.tpl");
    }
    
    /**
     * 获取门店数据展示对话框
     */
    public void dialogStore(){
        int pageNumber = getParaToInt(Constant.Ui.PAGER_NUMBER, 1);
        int pageSize = getParaToInt(Constant.Ui.PAGER_SIZE, 10);
        String operation = getPara(KEY_OPERATION);
        setAttr(KEY_OPERATION, operation);
        String flowId = getPara(KEY_FLOW_ID);
        setAttr(KEY_FLOW_ID, flowId);
        int module_id = getParaToInt(KEY_MODULEID, 0);
        int pay_plat = getParaToInt(KEY_PAYPLAT, 0);
        setAttr(KEY_MODULEID, module_id);
        setAttr(KEY_PAYPLAT, pay_plat);
      
        ConcurrentHashMap<String, String> map = getSearchInfo();
        setAttr(Constant.Ui.PAGER, WebsiteService.service.getStore(pageNumber, pageSize, map,operation,
        		module_id, pay_plat, flowId));
        render("/admin/website_pay_auth_dialog_store.tpl");
    }
    
    /**
     * 获取人员数据展示对话框
     */
    public void dialogMember(){
        int pageNumber = getParaToInt(Constant.Ui.PAGER_NUMBER, 1);
        int pageSize = getParaToInt(Constant.Ui.PAGER_SIZE, 10);
        String operation = getPara(KEY_OPERATION);
        setAttr(KEY_OPERATION, operation);
        String flowId = getPara(KEY_FLOW_ID);
        setAttr(KEY_FLOW_ID, flowId);
        int module_id = getParaToInt(KEY_MODULEID, 0);
        int pay_plat = getParaToInt(KEY_PAYPLAT, 0);
        setAttr(KEY_MODULEID, module_id);
        setAttr(KEY_PAYPLAT, pay_plat);
    
        ConcurrentHashMap<String, String> map = getSearchInfo();
        setAttr(Constant.Ui.PAGER, WebsiteService.service.getMember(pageNumber, pageSize, map,operation,
        		module_id, pay_plat, flowId));
        render("/admin/website_pay_auth_dialog_member.tpl");
    }
    
    /**
     * 获取渠道类数据展示对话框
     */
    public void dialogChannel(){
        int pageNumber = getParaToInt(PAGE_NUMBER, 1);
        int pageSize = getParaToInt(PAGE_SIZE, 10);
        ConcurrentHashMap<String, String> map = getSearchInfo();
        int type = getParaToInt(KEY_TYPE);// 根据这个查询是渠道大类，渠道簇还是渠道小类type-3即是对应的层级
        setAttr(KEY_TYPE, type);
        String operation = getPara(KEY_OPERATION);
        setAttr(KEY_OPERATION, operation);
        String flowId = getPara(KEY_FLOW_ID);
        setAttr(KEY_FLOW_ID, flowId);
        int module_id = getParaToInt(KEY_MODULEID, 0);
        int pay_plat = getParaToInt(KEY_PAYPLAT, 0);
        setAttr(KEY_MODULEID, module_id);
        setAttr(KEY_PAYPLAT, pay_plat);
   
        Page<Record> page = WebsiteService.service.getChannel(type, pageNumber, pageSize, map,operation,
        		module_id, pay_plat, flowId);
        setAttr(Constant.Ui.PAGER, page);
        render("/admin/website_pay_auth_dialog_channel.tpl");
    }

    
    /**
     * 新增，按照地市、门店、人员、渠道大类、渠道簇、渠道小类配置
     */
    public void addAuthOrizationByType(){
        String flowId = getPara(KEY_FLOW_ID);
        int type = getParaToInt(KEY_TYPE);
        String operation = getPara(KEY_OPERATION);
      
        Integer[] ids = Utils.string2Int(getPara("ids"), "#");
        
        boolean ret = false;
//        if("add".equalsIgnoreCase(operation)){// 新增的新增
//            ret = BusinessAuthConfigService.service.saveByTypeTemp(flowId, type, ids);
//        } else if("edit".equalsIgnoreCase(operation)){// 编辑的新增
            int module_id = getParaToInt(KEY_MODULEID);
            int pay_plat = getParaToInt(KEY_PAYPLAT);
         
           
            ret = WebsiteService.service.saveByType(type, ids, module_id, pay_plat);
        //}
        if(ret){
            ajaxJsonSuccessMessage("OK");
        } else{
            ajaxJsonErrorMessage("FAIL");
        }
    }
    
    /**
     * 统一的删除方法,通过operation区分，由于设计的原因，新增的删除的id是实体类的id，编辑的删除id是关系表的id
     */
    public void deleteAuthorization(){
    
        Integer[] ids = Utils.string2Int(getPara("ids"), "#");
        String operation = getPara(KEY_OPERATION);
        int type = getParaToInt(KEY_TYPE);
        setAttr(KEY_OPERATION, operation);
        setAttr(KEY_TYPE, type);
        boolean ret = false;
     
//        if("add".equalsIgnoreCase(operation)){// 新增的删除
//            String flowId = getPara(KEY_FLOW_ID);
//            //ret = WebsiteService.service.deleteByTypeTemp(type, flowId, ids);
//        } else if("edit".equalsIgnoreCase(operation)){// 编辑的删除
            ret = WebsiteService.service.deleteByType(ids);
        //}
        if(ret){
            ajaxJsonSuccessMessage("OK");
        } else{
            ajaxJsonErrorMessage("FAIL");
        }
    }

    public void findByPageForSearch4Website(String select, String sqlExceptSelect){
        // System.out.println("sql = " + select + sqlExceptSelect);
        Page<Record> pager = Db.paginate(getParaToInt("pageNumberWebsite", 1), getParaToInt(PAGE_SIZE, 10), select,
                                         sqlExceptSelect);
        setAttr("websitePage", pager);
    }

    // public void payConfig(){
    // int moduleId = getParaToInt("moduleId");
    // int pay = getParaToInt("pay");
    // int city_org_id = InterfaceUtil.getCityIdByOrgId(InterfaceUtil.getOrgId()
    // + "");
    // String[] ids = getParaValues("ids");
    // WebsiteService.service.updateWebsites(moduleId, pay, city_org_id, ids);
    // redirect("/website/payMethods");
    // }
    public void payConfig(){
        int moduleId = getParaToInt("moduleId");
        int payId = getParaToInt("payId");

        keepPara();

        int city_org_id = InterfaceUtil.getOrgId();
        if(city_org_id != InterfaceUtil.getIdTop()){// 地市级管理员取地市orgId，省级管理员取自身orgId
            city_org_id = InterfaceUtil.getCityIdByOrgId(city_org_id + "");
        }
        String[] ids = getParaValues("idsWebsite");
        WebsiteService.service.updateWebsites(moduleId, payId, city_org_id, ids);

        keepPara();
        JSONObject obj = new JSONObject();
        obj.put("result", "OK");
        renderText(obj.toString());
    }

    public void deletePayWebsite(){
        String[] ids = getParaValues("idsWebsite");

        keepPara();

        ResultCode result = WebsiteService.service.deletePayWebsite(ids);

        if(result == ResultCode.SUCCESS){
            ajaxJsonSuccessMessage(Constant.Msg.DEL_DONE);
        } else if(result == ResultCode.PARAM_ERROR){
            ajaxJsonErrorMessage(Constant.Msg.DEL_FAILED);
        } else if(result == ResultCode.FAILED){
            ajaxJsonErrorMessage(Constant.Msg.DEL_FAILED);
        }
    }
    
    
    /**
     * 展示渠道类型页面/website/showWebsites/{parentId}
     */
    public void showWebsites(){
        Integer parentId = getParaToInt();
        if(null == parentId){parentId = 0;}
        int pageNumber = getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT);
        int pageSize = getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT);
        String class_name = getPara("class_name");
        keepPara();
        setAttr("parent",WebsiteService.service.findById(parentId));
        
        setAttr("pager", WebsiteService.service.getWebsitesByParentId(parentId,class_name, pageNumber, pageSize));
        render("/admin/website_show.tpl");
    }
}
